package com.weitu.permission.authority.controller;

import com.weitu.base.annotaion.SystemLog;
import com.weitu.base.controller.BaseController;
import com.weitu.paging.DataGridPagination;
import com.weitu.permission.authority.entity.AuthorityGroup;
import com.weitu.permission.authority.service.AuthorityGroupService;
import com.weitu.permission.authority.service.AuthorityService;
import com.weitu.plugin.PageView;
import com.weitu.util.AjaxResult;
import com.weitu.util.DateTimeUtil;
import com.weitu.util.IDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by WT on 2017/3/17.
 */



@Controller
@RequestMapping(value = "permission/authorityGroup")
public class AuthorityGroupController extends BaseController {

    private static final String MODULE_BATH_PATH = BASE_JSP_PATH + "permission/authorityGroup";
    private final AuthorityGroupService authorityGroupService;
    private final AuthorityService authorityService;

    @Autowired
    public AuthorityGroupController(AuthorityGroupService authorityGroupService,AuthorityService authorityService) {
        this.authorityGroupService = authorityGroupService;
        this.authorityService = authorityService;
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list() {
        return MODULE_BATH_PATH + "/list";
    }

    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Object listAjax(HttpServletRequest request) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        putParametersIntoMap(map);

        //查询数据总数
        int count = this.authorityGroupService.queryCount(map);
        //获取分页对象
        DataGridPagination Pagination = DataGridPagination.getInstance(request, count);

        PageView pageView = Pagination.getPageView();//生成pageView分页对象
        //未传sort参数是采用添加时间排序
        if (map.get("sort") == null) {
            pageView.setSortColumn("addTime");
        }
        map.put("pageView", pageView);

        List<AuthorityGroup> dataList = this.authorityGroupService.queryList(map);

        return Pagination.flushDataGridData(dataList);
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String addView() {
        return MODULE_BATH_PATH + "/add";
    }

    @RequestMapping(value = "/edit" ,method = RequestMethod.GET)
    public ModelAndView editView(String id) {

        AuthorityGroup authorityGroup=authorityGroupService.selectAuthorityGroupById(id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("authorityGroup", authorityGroup);
        modelAndView.setViewName(MODULE_BATH_PATH + "/edit");

        return modelAndView;
    }

    /**
     * 插入操作
     * @param authorityGroup
     * @return
     */
    @ResponseBody
    @RequestMapping(value ="/add", method = RequestMethod.POST)
    @Transactional(readOnly = false)
    @SystemLog(module = "权限组管理",methods = "添加权限组")
    public AjaxResult insertSelective(AuthorityGroup authorityGroup) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        putParametersIntoMap(paramMap);
        AjaxResult result = new AjaxResult();
        String id = IDGenerator.getId();
        String addTime=DateTimeUtil.formatterDateTime();
        authorityGroup.setId(id);
        authorityGroup.setAddTime(addTime);
        int boo = authorityGroupService.insertSelective(authorityGroup);
        if (boo == 1) {
            result.setMessage("保存成功");
            result.setSuccess(true);
            return result;
        }else {
            result.setMessage("保存失败！");
            result.setSuccess(false);
            return result;
        }
    }


    /**
     * 删除操作
     */
    @ResponseBody
    @RequestMapping("deleteById")
    @Transactional(readOnly = false)
    @SystemLog(module = "权限组管理",methods = "删除权限组")
    public AjaxResult deleteById(String id){
        Map<String, Object> paramMap = new HashMap<String, Object>();
        String groupId=id;
        paramMap.put("groupId",groupId);
        //根据groupId获取权限组下权限数
        int count=authorityService.queryTotalCount(paramMap);
        AjaxResult result=new AjaxResult();

        if (count>0){
            result.setMessage("所选权限组下已分配权限信息，不可以删除！");
            result.setSuccess(false);
            return result;
        }else {
            int boo=authorityGroupService.deleteById(id);
            if (boo==1){
                result.setMessage("删除成功");
                result.setSuccess(true);
                return  result;
            } else {
                result.setMessage("删除失败！");
                result.setSuccess(false);
                return result;
            }
        }

    }

    /**
     *更新
     */
    @ResponseBody
    @RequestMapping(value ="/edit", method = RequestMethod.POST)
    @Transactional(readOnly=false)//需要事务操作必须加入此注解
    @SystemLog(module = "权限组管理",methods = "添加权限组")
    public AjaxResult edit(AuthorityGroup authorityGroup){
        AjaxResult result = new AjaxResult();
        String updateTime=DateTimeUtil.formatterDateTime();
        authorityGroup.setUpdateTime(updateTime);
        int boo=authorityGroupService.updateByIdSelective(authorityGroup);
        if(boo==1){
            result.setMessage("保存成功！");
            result.setSuccess(true);
            return result;
        } else {
            result.setMessage("保存失败！");
            result.setSuccess(false);
            return result;
        }
    }

    /**
     * 限制code唯一性
     * @param code
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectByCode")
    public AjaxResult findByCode(String code) {

        AuthorityGroup authorityGroup=  authorityGroupService.selectAuthorityGroupByCode(code.trim());

        AjaxResult ajaxResult=new AjaxResult();
        if (null == authorityGroup){
            ajaxResult.setSuccess(true);
            ajaxResult.setMessage("标识可用");
        }else {
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("标识已存在");
        }

        return ajaxResult;
    }

    /**
     * 获取权限组未分页数据
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getAuthorityGroupList",method = RequestMethod.POST)
    public Object listAll(){
        List<AuthorityGroup> dataList = this.authorityGroupService.getAuthorityGroupList();
        return dataList;
    }




}



